﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ActivitiesControl.Domain.Services;
using ActivitiesControl.Domain.Utils;
using Entities = ActivitiesControl.Domain.Model;
using ActivitiesControl.Domain.Model;

namespace ActivitiesControl.GUI.Desktop.Forms
{
	public partial class AuthenticationForm
		: Form
	{
		public event Action<User> Authenticated;
		
		public AuthenticationForm()
		{
			InitializeComponent();
			textUsername.Text = Environment.UserName;
			textPassword.Focus();
			ServicesLocator.UserServices.MessageRaised += new MessageHandler(service_MessageRaised);
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			User user = null;

			if (ServicesLocator.UserServices.ValidateUser(textUsername.Text, textPassword.Text, out user))
			{
				Authenticated(user);
			}
		}

		void service_MessageRaised(object sender, MessageEventArgs e)
		{
			if (e.Message.Type != MessageType.Ok)
				MessageBox.Show(e.Message.Text, e.Message.Title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
			else
				ServicesLocator.UserServices.MessageRaised -= new MessageHandler(service_MessageRaised);
		}

		private void txt_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Return)
				btnOk_Click(null, null);
		}

        private void Start_Load(object sender, EventArgs e)
        {

        }
	}
}
